package org.jim.server.demo.config;

import org.apache.commons.lang3.StringUtils;
import org.jim.core.utils.PropUtil;
import org.jim.server.JimServer;
import org.jim.server.config.ImServerConfig;
import org.jim.server.config.PropertyImServerConfigBuilder;
import org.jim.server.demo.listener.ImDemoGroupListener;
import org.jim.server.demo.listener.ImDemoUserListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.tio.core.ssl.SslConfig;

import java.io.IOException;

/**
 *  IM 服务初始化加载
 *
 *  Im 加载配置类
 *
 * @author lml
 */
@Configuration
public class JimServerConfiguration {

    private static final Logger log = LoggerFactory.getLogger(JimServerConfiguration.class);

    /**
     * 先加载 ImServerConfig 配置文件
     */
    @Bean
    @Primary// 用来标识 配置加载顺序；优先加载
    public ImServerConfig imServerConfig() throws Exception {
        log.info("============================IM 服务初始化加载配置文件 开始！！！！！===========================");
        ImServerConfig config = new PropertyImServerConfigBuilder("config/jim.properties").build();
        log.info("============================开始读取配置文件：config/jim.properties ===========================");        // 初始化配置
        initSsl(config);
        /**
         * 设置群组监听器，非必须，根据需要自己选择性实现;
         */
        log.info("设置群组监听器，非必须，根据需要自己选择性实现");
        config.setImGroupListener(new ImDemoGroupListener());
        /**
         * 设置绑定用户监听器，非必须，根据需要自己选择性实现;
         */
        log.info("设置绑定用户监听器，非必须，根据需要自己选择性实现");
        config.setImUserListener(new ImDemoUserListener());
        return config;
    }

    /**
     * 启动IM 服务
     */
    @Bean
    public JimServer jimServer(ImServerConfig imServerConfig) throws IOException {

        log.info("============================开始启动 IM 服务===========================");
        JimServer server = new JimServer(imServerConfig);
        server.start();
        log.info("============================IM 服务启动完成===========================");
        log.info("============================IM 监听地址:{},监听端口: {}===========================",
                imServerConfig.getBindIp(),imServerConfig.getBindPort());
        return server;
    }

    /**
     * 开启SSL之前，你要保证你有SSL证书哦！
     * @param imServerConfig
     * @throws Exception
     */
    private static void initSsl(ImServerConfig imServerConfig) throws Exception {

        log.info("============================开启SSL; [开启SSL之前，你要保证你有SSL证书哦！] ===========================");
        //开启SSL
        if(ImServerConfig.ON.equals(imServerConfig.getIsSSL())){
            String keyStorePath = PropUtil.get("jim.key.store.path");
            String keyStoreFile = keyStorePath;
            String trustStoreFile = keyStorePath;
            String keyStorePwd = PropUtil.get("jim.key.store.pwd");
            if (StringUtils.isNotBlank(keyStoreFile) && StringUtils.isNotBlank(trustStoreFile)) {
                SslConfig sslConfig = SslConfig.forServer(keyStoreFile, trustStoreFile, keyStorePwd);
                imServerConfig.setSslConfig(sslConfig);
            }
        }
    }
}